var i;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var ResKeeper = require("AA_ResKeeper");
var Logic_ChildMoveEvent = require("AA_Logic_ChildMoveEvent");
var Item_GameBagCell = require("AA_Item_GameBagCell");
var _decorator = cc._decorator;
var _ccclass = _decorator.ccclass;
var _property = _decorator.property;

var _def_Item_CellFatherMove = function (e) {
  function _creator() {
    var t = null !== e && e.apply(this, arguments) || this;
    t.cellFatherNode = null;
    t.lastCubeArray = [];
    return t;
  }
  __extends(_creator, e);
  _creator.prototype.StartSet = function (e) {
    this.uiBag = e;
    this.childMove = this.cellFatherNode.getComponent(Logic_ChildMoveEvent.default);
    this.childMove || (this.childMove = this.cellFatherNode.addComponent(Logic_ChildMoveEvent.default));
    this.childMove.StartSet(this.onChildTouchStart.bind(this), this.onChildTouchMove.bind(this), this.onChildTouchUp.bind(this));
    this.childMove.SetCantClick(false);
  };
  _creator.prototype.SetCanMove = function (e, t) {
    if (e) {
      if (t) {
        var o = this.uiBag.auto_ui.node_cubeBaseFather.children[this.cellFatherNode.children[0].getComponent(Item_GameBagCell.default).cellData.cellIndex].convertToWorldSpaceAR(cc.v2(0, 0));
        var n = this.cellFatherNode.children[0].convertToWorldSpaceAR(cc.v2(0, 0));
        var i = cc.v2(o.x - n.x, o.y - n.y);
        this.node.x += i.x;
        this.node.y += i.y;
        this.startPos = this.node.getPosition();
        this.childMove.SetCantClick(true);
      }
      this.checkEndData = null;
    } else {
      this.childMove.SetCantClick(false);
    }
  };
  _creator.prototype.onChildTouchStart = function () {
    if (this.guidePos) {
      this.node.stopAllActions();
      this.node.setPosition(this.guidePos);
      this.guidePos = null;
      this.uiBag.ShowGuide8_2();
      this.childMove.SetCantClick(false);
      return void (this.node.opacity = 255);
    }
  };
  _creator.prototype.onChildTouchMove = function (e) {
    this.node.x += e.getDeltaX();
    this.node.y += e.getDeltaY();
    this.CheckCellIsOk();
  };
  _creator.prototype.onChildTouchUp = function () {
    var e = this;
    var t = this.CheckCellIsOk();
    this.lastCubeArray.forEach(function (t) {
      e.uiBag.baseCellCompArray[t].SetBgNodeColor(cc.Color.WHITE);
    });
    if (t.isOk) {
      this.checkEndData = t;
      this.node.x += t.effectPos.x;
      this.node.y += t.effectPos.y;
      this.startPos = this.node.getPosition();
      this.uiBag.MoveChangeBagCellIndex(this.checkEndData);
    } else {
      this.node.setPosition(this.startPos);
      this.checkEndData = null;
    }
    this.lastCubeArray.length = 0;
  };
  _creator.prototype.CheckCellIsOk = function () {
    var e = this;
    this.lastCubeArray.forEach(function (t) {
      e.uiBag.baseCellCompArray[t].SetBgNodeColor(cc.Color.WHITE);
    });
    var t = this.cellFatherNode.children;
    var o = this.uiBag.auto_ui.node_cubeBaseFather.children;
    var n = [];
    var i = [];
    var a = [];
    t.forEach(function (e) {
      n.push(e.convertToWorldSpaceAR(cc.v2(0, 0)));
    });
    o.forEach(function (e) {
      i.push(e.convertToWorldSpaceAR(cc.v2(0, 0)));
    });
    var r = true;
    var c = null;
    this.lastCubeArray.length = 0;
    for (var s = 0; s < t.length; s++) {
      var u = n[s];
      var l = false;
      var d = function (e) {
        var t = false;
        p.uiBag.currAddCellDataMap.size > 0 && p.uiBag.currAddCellDataMap.forEach(function (o) {
          if (o.pickCellArray && !t) {
            for (var n = 0; n < o.pickCellArray.length; n++) {
              var i = o.pickCellArray[n];
              if (e == i.cellData.cellIndex) {
                t = true;
                break;
              }
            }
          }
        });
        if (t) {
          return "continue";
        }
        var o = i[e];
        if (cc.Vec2.distance(u, o) < p.uiBag.effectMax) {
          l = true;
          p.lastCubeArray.push(e);
          a.push(e);
          c || (c = cc.v2(o.x - u.x, o.y - u.y));
          return "break";
        } else {
          return undefined;
        }
      };
      var p = this;
      for (var h = 0; h < o.length && "break" !== d(h); h++) {
        ;
      }
      l || (r = false);
    }
    this.lastCubeArray.forEach(function (t) {
      e.uiBag.baseCellCompArray[t].SetBgNodeColor(r ? cc.Color.GREEN : cc.Color.RED);
    });
    return {
      isOk: r,
      effectPos: c,
      pickCellArray: a
    };
  };
  _creator.prototype.ShowGuideTween = function () {
    this.guidePos || (this.guidePos = this.node.getPosition());
    this.node.stopAllActions();
    this.node.runAction(cc.repeatForever(cc.sequence(cc.spawn(cc.moveTo(1, this.guidePos.x - 100, this.guidePos.y), cc.fadeIn(.2)), cc.callFunc(function () {}), cc.spawn(cc.moveTo(.2, this.guidePos.x, this.guidePos.y), cc.fadeOut(.2)), cc.callFunc(function () {}))));
  };
  __decorate([_property(cc.Node)], _creator.prototype, "cellFatherNode", undefined);
  return __decorate([_ccclass], _creator);
}(ResKeeper.default);
exports.default = _def_Item_CellFatherMove;